//go:build linux || windows || darwin

package main

import (
	"flag"
	"fmt"
	"github.com/zeromicro/go-zero/core/logx"
	"github.com/zeromicro/go-zero/rest/httpx"
	"net/http"
	"product/authorization/serve/internal/config"
	"product/authorization/serve/internal/handler"
	"product/authorization/serve/internal/svc"
	"product/common/utils"

	"github.com/zeromicro/go-zero/core/conf"
	"github.com/zeromicro/go-zero/rest"
)

// 跨域常量设置
const (
	// AccessControlAllowHeaders 允许跨域请求头
	AccessControlAllowHeaders = "Content-Type,AccessToken,X-CSRF-Token,Authorization,Token,X-Token,X-User-Id,OS,Platform, Version"
	// AccessControlAllowMethods 允许跨域请求方法
	AccessControlAllowMethods = "POST, GET, OPTIONS, PUT, DELETE,UPDATE"
	// AccessControlExposeHeaders  允许跨域响应头
	AccessControlExposeHeaders = "Content-Length,Content-Type"
)

var configFile = flag.String("f",
	"etc/Authorization.yaml",
	"the config file")

func main() {
	flag.Parse()

	var c config.Config
	conf.MustLoad(*configFile, &c)
	// 启用日志配置
	logx.MustSetup(c.LogConf)

	// 配置跨域
	server := rest.MustNewServer(c.RestConf, rest.WithCustomCors(func(header http.Header) {
		header.Add("Access-Control-Allow-Headers",
			AccessControlAllowHeaders)
		header.Set("Access-Control-Allow-Methods",
			AccessControlAllowMethods)
		header.Set("Access-Control-Expose-Headers",
			AccessControlExposeHeaders)
	}, nil, c.CorsConf.AllowOrigins...))
	defer server.Stop()

	ctx := svc.NewServiceContext(c)
	handler.RegisterHandlers(server, ctx)

	// 统一错误处理
	httpx.SetErrorHandlerCtx(utils.ErrorHandler(c.Name))
	fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
	server.Start()
}
